# 12/12
# 文件的创建
f = open(r'C:\Users\Administrator\Desktop\520.txt',encoding='utf-8')
print(f.read())
# with open读取文件
with open(r'C:\Users\Administrator\Desktop\520.txt',encoding='utf-8') as file:
    text = f.read()
print(text)

# 写文件write()
date = 'hello world'
f1 = open('333.txt','w',encoding='utf-8')
f1.write('helloween,helloween')
f1.close()

# 打开文件用于写入，如果文件不存在将会被创建
with open('333.txt','w',encoding='utf-8') as file:
    file.write(date)

# 处理excel数据
import pandas as pd
df = pd.read_excel(r'C:\Users\Administrator\Desktop\111.xlsx')
print(df)
# 写文件到excel
a1 = {"name": "xiaohong", "nianling": 20}
a2 = {"name": "xiaoliu", "nianling": 20}
a3 = {"name": "xiaobai", "nianling": 20}
l = [a1, a2, a3]
# 将列表变成dateframe
df1 = pd.DataFrame(l)
print(df1)
# 使用pandas函数.to_excel()  index=False   不索引
df1.to_excel("111.xlsx",index=False)

# 处理world文档
# 导入docx库中的Document模块
from docx import Document
# 打开docx文档，获取文档对象
docx = Document('2023年国务院政府工作报告.docx')
print(docx)
# 获取world段落，使用paragraphs，得到的是列表
p = docx.paragraphs
print(p)
print(len(p))
# 遍历列表，使用text，显示段落内容
for i in p:
    print(i.text)